<?php
use_helper('Text');

$tab_news = array('main_image' => "", 'thumb_image' => "", 'old_news' => "");
$li = null;
$i = 0;

foreach($news_list as $news){

    $validityTimePost = strtotime($news['updated_at']) + 5*24*60*60;
    $isValid = time() < $validityTimePost ? true : false;

    if($i < 4) {
        $imagesAttributes = Utils::getImageSize($news['sfAsset'][ 'Folder' ][ 'relative_path' ], $news['sfAsset'], 497, 248 );

        $url = "#";
        $width = '0';
        $height = '0';
        $marginTop = '0';
        $marginLeft = '0';

        if(!is_null($imagesAttributes['large']['url'])){
            $url = $imagesAttributes['large']['url'];
            $width = $imagesAttributes['large']['width'];
            $height = $imagesAttributes['large']['height'];
            $marginTop = $imagesAttributes['large']['marginTop'];
            $marginLeft = $imagesAttributes['large']['marginLeft'];
        }

        $tab_news['main_image'] .=
           '<li class="l_' . $i . ( $i == 0 ? ' active' : '' ) . '">'
                . '<img src="' . $url . '" title="' . $news['title'] . '" alt="' . $news['title'] . '" width="' . $width . '" height="' . $height . '" style="margin-top: ' . $marginTop . 'px; margin-left: ' . $marginLeft . 'px;" />'
                . '<div class="desc">
                        <div class="block">
                            <h3>' . truncate_text($news['title'], 40, '...') .'</h3>
                            <small>Posté le ' . date("d/m/Y", strtotime($news['updated_at'])) . ' par ' . $news['sfGuardUser01']['username'] . '</small>
                            <p>' . truncate_text(strip_tags($news->getRaw('text')), 200, '...') .'</p>
                            <a class="next" href="' . url_for(array('module' => 'news', 'action' => 'index', 'year' => date("Y", strtotime($news['updated_at'])), 'month' => date("m", strtotime($news['updated_at'])), 'title_slug' => $news['slug'])) . '">&#187; Lire la suite</a>
                        </div>
                    </div>'
         . '</li>';

        $tab_news['thumb_image'] .= '<li class="l_' . $i . ( $i == 0 ? ' active' : '' ) . '">';
        $tab_news['thumb_image'] .= '<h3><a href=' . url_for(array('module' => 'news', 'action' => 'index', 'year' => date("Y", strtotime($news['updated_at'])), 'month' => date("m", strtotime($news['updated_at'])), 'title_slug' => $news['slug'])) . '>
                                    ' . truncate_text($news['title'], 40, '...') .'
                                    </a></h3>';
        if($isValid){
            $tab_news['thumb_image'] .= '<span class="new">Nouveau !</span>';
        }
        $tab_news['thumb_image'] .= '<small>Posté le ' . date("d/m/Y", strtotime($news['updated_at'])) . ' par ' . $news['sfGuardUser01']['username'] . '</small>
                                    </li>';

    }else{
        $imagesAttributes = Utils::getImageSize($news['sfAsset'][ 'Folder' ][ 'relative_path' ], $news['sfAsset'], 63, 36 );
        
        $tab_news['old_news'] .=
        '<li>
            <h3><a title="' . $news['title'] . '" href="' . url_for(array('module' => 'news', 'action' => 'index', 'year' => date("Y", strtotime($news['updated_at'])), 'month' => date("m", strtotime($news['updated_at'])), 'title_slug' => $news['slug'])) . '">
                ' . truncate_text($news['title'], 30, '...') .'
            </a></h3>
            <span>Posté le ' . date("d/m/Y", strtotime($news['updated_at'])) . '<br/>par ' . $news['sfGuardUser01']['username'] . '<span class="nb-com">' . $news['nb_comments'] . '</span></span>
                
            <div class="thumb">'
               . image_tag($imagesAttributes['small']['url'], array('title' => $news['title'], 'alt' => $news['title'], 'width' => $imagesAttributes['small']['width'], 'height' => $imagesAttributes['small']['height']))
           . '</div>
          </li>';
    }
    $i++;
}

$kindNews = isset($kindNews) ? $kindNews : "";
switch($kindNews){
    case "adult":
        $kind_title = "Actualités adultes ";
        break;
    case "young":
        $kind_title = "Actualités jeunes ";
        break;
    default:
        $kind_title = "";
        break;
}
$kind_title .= "&#187; ";
?>

<div id="home-news" class="shadowed">
    <div class="inner-boundary">

        <ul class="news-type">
            <li<?php echo (empty($kindNews) ? ' class="selected"' : '') ?>><a href="<?php echo url_for('@default_news_homepage') ?>" onClick="loadKindNews(this); return false;" >Toute l'actualité</a></li>
            <li<?php echo ($kindNews == 'adult' ? ' class="selected"' : '') ?>><a href="<?php echo url_for('@adult_news_homepage') ?>" onClick="loadKindNews(this); return false;" >Actualités adultes</a></li>
            <li<?php echo ($kindNews == 'young' ? ' class="selected"' : '') ?>><a href="<?php echo url_for('@young_news_homepage') ?>" onClick="loadKindNews(this); return false;" >Actualités jeunes</a></li>
        </ul>

        <div class="push_float"></div>
        <h4><?php echo $kind_title ?> A la une</h4>
        <div class="slide-news">
            <div class="main_image">
                <ul>
                    <?php echo $tab_news['main_image'] ?>
                </ul>
            </div>

            <div class="image_thumb">
                <ul>
                    <?php echo $tab_news['thumb_image'] ?>
                </ul>
            </div>

        </div>
        <h4><?php echo $kind_title ?> Le reste de l'actu</h4>
        <ul class='other-news'>
            <?php echo $tab_news['old_news'] ?>
        </ul>
        <div class="push_float"></div>
        <p class="link_all_news">Découvrez toute <?php echo link_to('l\'actualité de CPF Aizenay', '@news_all') ?></p>
    </div>
</div>